package com.hexacta.hrs.test.data;

import static com.hexacta.hrs.test.data.CompanyTestDataDependencyCreator.HEXACTA_COMPANY;
import static com.hexacta.hrs.test.data.JobTermTestDataDependencyCreator.UNASSIGNED_JOB_TERM;
import static com.hexacta.hrs.test.data.PersonTestDataDependencyCreator.ADMIN_PERSON;

import java.util.Calendar;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.vectrics.company.Company;
import org.vectrics.employee.Employee;
import org.vectrics.recruiter.job.JobTerm;
import org.vectrics.user.Person;

/**
 * Test data dependency creator for the Employee class.
 * <p>
 * TODO We should review this if we want more meaningful data.
 * 
 * @author tpollak
 */
public class EmployeeTestDataDependencyCreator extends AbstractTestDataDependencyCreator {

    public static final String EMPLOYEE_1 = "EMPLOYEE_1";

    public static final String EMPLOYEE_2 = "EMPLOYEE_2";

    public static final String EMPLOYEE_3 = "EMPLOYEE_3";

    public static final String EMPLOYEE_4 = "EMPLOYEE_4";

    public static final String EMPLOYEE_5 = "EMPLOYEE_5";

    public EmployeeTestDataDependencyCreator(List<TestDataDependencyCreator> dependencies) {
        super(dependencies);
    }

    public void createDependentTestData(Map<String, Object> context) {
        // Create one domain for Hexacta
        Employee employee1 = this.createEmployee(context, PersonTestDataDependencyCreator.PERSON_1);
        Employee employee2 = this.createEmployee(context, PersonTestDataDependencyCreator.PERSON_2);
        Employee employee3 = this.createEmployee(context, PersonTestDataDependencyCreator.PERSON_3);
        Employee employee4 = this.createEmployee(context, PersonTestDataDependencyCreator.PERSON_4);
        Employee employee5 = this.createEmployee(context, PersonTestDataDependencyCreator.PERSON_5);

        employee1.setElegibleMentor(Boolean.TRUE);
        employee3.setElegibleMentor(Boolean.TRUE);

        employee1 = this.insertDomainObject(employee1);
        employee2 = this.insertDomainObject(employee2);
        employee3 = this.insertDomainObject(employee3);
        employee4 = this.insertDomainObject(employee4);
        employee5 = this.insertDomainObject(employee5);

        this.assignMentor(employee2, employee1);
        this.assignMentor(employee3, employee1);
        this.assignMentor(employee4, employee3);
        this.assignMentor(employee5, employee3);

        employee1 = this.insertDomainObject(employee1);
        employee2 = this.insertDomainObject(employee2);
        employee3 = this.insertDomainObject(employee3);
        employee4 = this.insertDomainObject(employee4);
        employee5 = this.insertDomainObject(employee5);

        this.storeObjectReferenceInContext(context, EMPLOYEE_1, employee1);
        this.storeObjectReferenceInContext(context, EMPLOYEE_2, employee2);
        this.storeObjectReferenceInContext(context, EMPLOYEE_3, employee3);
        this.storeObjectReferenceInContext(context, EMPLOYEE_4, employee4);
        this.storeObjectReferenceInContext(context, EMPLOYEE_5, employee5);
    }

    @Override
    public Set<Scenario> getApplicableScenarios() {
        return EnumSet.of(Scenario.DEVELOPMENT);
    }

    private Employee createEmployee(Map<String, Object> context, String personToken) {
        Company hexacta = (Company) this.loadObjectFromContext(context, HEXACTA_COMPANY);
        Person admin = (Person) this.loadObjectFromContext(context, ADMIN_PERSON);
        Person person = (Person) this.loadObjectFromContext(context, personToken);
        JobTerm term = (JobTerm) this.loadObjectFromContext(context, UNASSIGNED_JOB_TERM);
        return this.getDataUtils().createEmployee(context, hexacta, admin, person, term);
    }

    private Employee assignMentor(Employee employee, Employee mentor) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, 2009);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 10);
        employee.addMentorAssignment(mentor, c.getTime());
        return employee;
    }
}